import {observable, action, computed, toJS} from 'mobx';
import {RouteLayout} from 'weaver-mobile-page';
import {Bubble,Toast} from 'weaver-mobile';
import * as odocSignApprovalApi from '../../apis/odocSignApprovalApi';
import * as odocEMSDK from '../../util/OdocEmSDK';
import * as pageTypes from "../../constants/OdocPageType";
// import {haoqianDatas} from '../../util/datas';

export default class OdocSignApprovalStore {

    @observable layout = new RouteLayout.Store({route: '/req/odocComs/'+pageTypes.ODOC_SIGN_APPROVAL+'/haoqian', id: "history"});

    @observable requestid = -1;
    @observable workflowid = -1;
    @observable nodeid = -1;
    @observable isNewHistory = false;

    @observable requestname = "";

    @observable ctx = null;
    @observable cssPath = "";
    @observable jsPath = "";
    @observable userid = -1;
    @observable lastname = "";
    @observable sequenceId = "";
    @observable sealName = "";
    @observable sealid = -1;
    @observable token = "";

    @observable isShowHistroy = false;//是否显示历史版本
    @observable isView = false;//只查看不可签批
    @observable isSeal = false;//可签批
    @observable isOpen = false;
    @observable isMust = false;
    @observable btns = [];//底部按钮
    @observable sealDocs = [];//签批文档
    @observable histroyDocs = [];//历史文档
    @observable isShowMore = false;

    readonly = false;
    @observable isShowQP = false;//是否展开签批的更多
    @observable isShowH = false;//是否展开历史版本更多

    @observable hasLoad = false

    @observable historyVersionId = -1;
    @observable historyPdfs = [];
    @observable historyImagefileId = -1;
    @observable imagefileid = -1;
    @observable previewUrl = "";
    @observable encodeimagefileid = "";
    @observable historyFileId = "";
    @observable historyDocName = "";
    @observable historyVersionName = "";
    @observable isViewHistory = false;

    @observable isSubmit = false;
    @observable isSave = true;
    @observable showHeader = true;
    @observable defaultSign = true;
    @observable defaultWriteColor="#C41018"//默认签批颜色
    @observable defaultWriteSize=3;//默认签批笔迹粗细
    @observable checkOut = -1;
    @observable checkOutUsername = "";
    @observable checkOutUserid = "";
    @observable isUsePDFViewer = false;
    @observable f_weaver_belongto_usertype = "";
    @observable f_weaver_belongto_userid="";
    @observable isSDK = 0;
    @observable sealImagefileid = "";
    @observable isDownload = false;
    @observable downloadBtnName = "";
    @observable qpSelected = false;

    @action
    convertFile=()=>{
        Toast.loading('Loading...', 10);
        let params = {
            fileid:this.imagefileid
        }
        odocSignApprovalApi.convertFile(params).then(data=>{
            if(data.data && data.data.length>0){

                this.previewUrl = data.data[0].path;
                document.getElementById("haoqianPdf").parentNode.removeChild(document.getElementById("haoqianPdf"));
                let pdfIframe = document.createElement('iframe');
                pdfIframe.id = "haoqianPdf";
                pdfIframe.src = data.data[0].path;
                pdfIframe.style.width = "100%";
                pdfIframe.style.height = "100%";
                document.getElementById("haoqianpdfboxinner").appendChild(pdfIframe);
                Toast.hide();
            }
        })
    }

    @action
    setProperty = (map) => {
        for (let i in map) {
            this[i] = map[i];
        }

    }

    @action
    getQianPiDatas = (newParams) => {
        Toast.loading('Loading...', 10);
        let isEm = window.navigator.userAgent.indexOf("E-Mobile7")>=0;
        let _that = this;
        let params = {
            workflowid: this.workflowid,
            requestid: this.requestid,
            nodeid: this.nodeid,
            isem:isEm ? 1 : 0,
            f_weaver_belongto_userid:this.f_weaver_belongto_userid,
            f_weaver_belongto_usertype:this.f_weaver_belongto_usertype,
            islock:1
        };
        if(newParams){
            params = newParams;
        }
        return odocSignApprovalApi.getHqSealInfo(params).then(data=>{
          if(data.api_status==0 || !data.sealDocs ||(data.sealDocs && data.sealDocs.length<=0)){
            Bubble.error("获取数据失败！",1000);
            setTimeout(function () {
              window.historyRoute.go(-1);
            },1000);
            return ;
          }
          this.setProperty(data);
          let firstDatas = data.sealDocs[0];
          this.sequenceId = firstDatas.sequenceId;
          this.sealName = firstDatas.imagefilename;
          this.sealid = firstDatas.id;
          this.sealImagefileid = firstDatas.encodeimagefileid;
          if(data.histroyDocs && data.histroyDocs.length>0){
              let firstHistoryDatas = data.histroyDocs[0];
              let historyPdf = firstHistoryDatas.imagefileEntities;
              this.historyPdfs=historyPdf;
              this.historyDocName=historyPdf[0].imagefilename;
              this.historyImagefileId=historyPdf[0].encodeimagefileid;
              this.imagefileid=historyPdf[0].imagefileid;
              this.historyFileId=historyPdf[0].id;
              this.historyVersionId=firstHistoryDatas.logid;
              this.historyVersionName=firstHistoryDatas.versionName+" (V"+data.histroyDocs.length+")";
          }else {
              if(data.sealDocs.length==1){
                  this.showHeader = false;
              }
          }
            Toast.hide();
          if(this.isSDK==0){
            odocEMSDK.setOdocNavTitle(data.requestname);
          }
        });
    }

    @action
    saveQianPiDatas = (buildNo) => {
        let isEm = window.navigator.userAgent.indexOf("E-Mobile7")>=0;
        let params = {
            workflowid: this.workflowid,
            requestid: this.requestid,
            nodeid: this.nodeid,
            sequenceId: this.sequenceId,
            buildNo: buildNo,
            imagefilename: this.sealName,
            sealid: this.sealid,
            isem:isEm ? 1 : 0,
            userid:this.userid,
            f_weaver_belongto_userid:this.f_weaver_belongto_userid,
            f_weaver_belongto_usertype:this.f_weaver_belongto_usertype
        };
        odocSignApprovalApi.saveH5SealDoc(params).then(data => {
            if (data.api_status == 1) {
                this.isSave = false;
                this.sealDocs = toJS(this.sealDocs).map((item) => {
                  if (item.sequenceId == this.sequenceId) {
                    item.buildNo = buildNo;
                    item.encodeimagefileid = data.encodeimagefileid;
                    item.imagefileid = data.imagefileid;
                    item.buildNo = buildNo;
                  }
                  return item;
                });
                this.sealImagefileid = data.encodeimagefileid;
                if (this.isSubmit) {
                    window.WfForm.doRightBtnEvent("BTN_SUBMIT");
                    window.historyRoute.go(-1);
                }
            } else {
                Bubble.error(data.msg, 1);
            }
        });
    }

    @action("签批签入签出")
    doCheckSignApprovalFile=(type)=>{
        let isEm = window.navigator.userAgent.indexOf("E-Mobile7")>=0;
        let params={
            checktype:type,
            requestid:this.requestid,
            userid:this.userid,
            isem:isEm ? 1 : 0,
            f_weaver_belongto_userid:this.f_weaver_belongto_userid,
            f_weaver_belongto_usertype:this.f_weaver_belongto_usertype
        };
        odocSignApprovalApi.checkSeal(params).then(data=>{})
    }

    @action
    resourceFiles = () => {
        return [
            this.cssPath,
            this.jsPath
        ]
    }
}